package com.kevin.hw.socket;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: kevin
 * @date: 2017/8/10
 * @description:
 */
public class UserServerThread implements Runnable {

    private User user;

    public UserServerThread(User user) {
        this.user = user;
    }

    public void run() {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();

            while (socket != null){
                ObjectInputStream bis = new ObjectInputStream(socket.getInputStream());

                String method = (String) bis.readObject();

                if(method.equals("age")){
                    int age = user.getAge();
                    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                    outputStream.writeInt(age);
                    outputStream.flush();
                }

            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
